import { CacheModule } from '@nestjs/cache-manager';
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AuthModule } from './auth/auth.module';
import { cacheConfig } from './config/cache.config';
import { Hotel } from './entities/hotel.entity';
import { Order } from './entities/order.entity';
import { Payment } from './entities/payment.entity';
import { Room } from './entities/room.entity';
import { User } from './entities/user.entity';
import { HotelsModule } from './hotels/hotels.module';
import { AuthHeaderMiddleware } from './middlewares/auth-header.middleware';
import { OrdersModule } from './orders/orders.module';
import { PaymentsModule } from './payments/payments.module';
import { RoomsModule } from './rooms/rooms.module';
import { UsersModule } from './users/users.module';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'Hzdx@2025',
      database: 'hotel_reservation',
      entities: [User, Hotel, Room, Order, Payment],
      synchronize: true, // 不要在生产环境使用
    }),
    CacheModule.register(cacheConfig),
    UsersModule,
    HotelsModule,
    RoomsModule,
    OrdersModule,
    PaymentsModule,
    AuthModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(AuthHeaderMiddleware).forRoutes('users/profile');
  }
}
